package com.sz.app.edi.rest.edi;

import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;

/**
 * AES CBC加密
 */
public class AESOperator {

    public static final int KEY_LEN = 16;
    /**
     * 加密用的Key 可以用26个字母和数字组成 此处使用AES-128-CBC加密模式，key需要为16位。
     */
    private static String KEY = "!QA2Z@w1sxO*(-8L";
    private static String VECTOR = "!WFNZFU_{H%M(S|a";
    private static AESOperator instance = null;

    private AESOperator() {

    }


    /**
     * 于内部静态类只会被加载一次，故该实现方式时线程安全的！
     */
    static private class Nested {
        private static AESOperator instance = new AESOperator();
    }

    public static AESOperator getInstance() {
        return Nested.instance;
    }

    /**
     * 加密
     *
     * @param content
     * @return
     * @throws Exception
     */
    public String encrypt(String content) throws Exception {
        return encrypt(content, KEY, VECTOR);
    }

    /**
     * 加密
     *
     * @param content
     * @return
     * @throws Exception
     */
    public String encrypt(String content, String key) throws Exception {
        return encrypt(content, key, VECTOR);
    }

    /**
     * 加密
     *
     * @param content 要加密的内容
     * @param key     秘钥
     * @param vector  加密向量 也是一个key
     * @return Srting
     * @throws Exception e
     */
    public String encrypt(String content, String key, String vector) throws Exception {
        if (key == null) {
            return null;
        }
        if (key.length() != KEY_LEN) {
            return null;
        }
        SecretKeySpec skeySpec = new SecretKeySpec(key.getBytes("UTF-8"), "AES");
        // 使用CBC模式，需要一个向量iv，可增加加密算法的强度
        IvParameterSpec iv = new IvParameterSpec(vector.getBytes());
        Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
        cipher.init(Cipher.ENCRYPT_MODE, skeySpec, iv);
        byte[] encrypted = cipher.doFinal(content.getBytes("UTF-8"));
        // 此处使用BASE64做转码。
        return new BASE64Encoder().encode(encrypted);
    }

    /**
     * 解密
     *
     * @param content
     * @return
     * @throws Exception
     */
    public String decrypt(String content) throws Exception {
        return decrypt(content, KEY, VECTOR);
    }

    /**
     * 解密
     *
     * @param content
     * @return
     * @throws Exception
     */
    public String decrypt(String content, String key) throws Exception {
        return decrypt(content, key, VECTOR);
    }

    /**
     * 解密
     *
     * @param content
     * @param key
     * @param vector
     * @return
     * @throws Exception
     */
    public String decrypt(String content, String key, String vector) throws Exception {
        try {
            if (key == null) {
                return null;
            }
            if (key.length() != KEY_LEN) {
                return null;
            }
            SecretKeySpec skeySpec = new SecretKeySpec(key.getBytes("UTF-8"), "AES");
            IvParameterSpec iv = new IvParameterSpec(vector.getBytes());
            Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
            cipher.init(Cipher.DECRYPT_MODE, skeySpec, iv);
            // 先用base64解密
            byte[] encrypted1 = new BASE64Decoder().decodeBuffer(content);
            byte[] original = cipher.doFinal(encrypted1);
            return new String(original, "UTF-8");
        } catch (Exception ex) {
            return null;
        }
    }

    public static void main(String[] args) throws Exception {
        // 需要加密的字串
        String cSrc = "{\"logisticsEvent\":{\"eventHead\":{\"eventCode\":\"00002\",\"eventSource\":\"63e581f6-4a2b-4bfb-aeba-9830718b1d14\",\"eventTarget\":\"hna\",\"token\":\"8792a8570ac84a03a65390d4a3ced55c10ca26da995a4684978742ce04b4d0ad\",\"eventTime\":\"2017-07-18 14:22:08\"},\"eventBody\":{\"tradeDetail\":{\"batchNo\":\"asdfasdfsaf\",\"orders\":{\"order\":[{\"waybillNo\":\"1707031554500155087128\",\"serviceOrderNo\":\"67567567895678745678\",\"code\":\"401\",\"status\":\"1\",\"messageTime\":\"2017-07-18 14:22:08\",\"trackMessage\":\"太原\",\"subOrderNo\":[\"ZD1707031552460155001953\",\"ZD1707031552460155001954\"]},{\"waybillNo\":\"1707031530490155071935\",\"serviceOrderNo\":\"675675678956787456232378\",\"code\":\"401\",\"status\":\"1\",\"messageTime\":\"2017-07-18 14:22:08\",\"trackMessage\":\"太原\",\"subOrderNo\":[\"ZD1707031530500155048952\"]}]}}}}}";
        // 加密
        long lStart = System.currentTimeMillis();
        String enString = AESOperator.getInstance().encrypt(cSrc, KEY);
        System.out.println("加密后的字串是：" + enString);

        long lUseTime = System.currentTimeMillis() - lStart;
        System.out.println("加密耗时：" + lUseTime + "毫秒");
        // 解密
        lStart = System.currentTimeMillis();
        String deString = AESOperator.getInstance().decrypt(enString);
        System.out.println("解密后的字串是：" + deString);
        lUseTime = System.currentTimeMillis() - lStart;
        System.out.println("解密耗时：" + lUseTime + "毫秒");
    }

}